//
//  WBHomePage.swift
//  Swift_Weibo
//
//  Created by Tim on 2019/8/13.
//  Copyright © 2019 Tim. All rights reserved.
//

import UIKit
import Moya
import RxSwift
import RxCocoa

private let cellIdentifier = "statusCell"

class WBHomePage: WBBaseViewController {
    
    /// 列表视图模型
    private let homeViewModel = WBHomeViewModel()
    
    /// 显示我的好友
    @objc private func showMyFriends() {
        // FIXME: - 此处待实现逻辑
        let testPage = WBTestPage()
        navigationController?.pushViewController(testPage, animated: true)
    }
}


// MARK: - 界面布局
extension WBHomePage {
    override func setupTableView() {
        super.setupTableView()
        navItem.leftBarButtonItem = UIBarButtonItem(title: "我的好友", target: self, action: #selector(showMyFriends))
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)
        
        homeViewModel.statuses.asDriver()
            .drive(tableView.rx.items(cellIdentifier: cellIdentifier, cellType: UITableViewCell.self)) { (row, element, cell) in
                cell.textLabel?.text = element.text
            }
            .disposed(by: disposeBag)
        
        homeViewModel.unreadCount()
            .asObservable()
            .subscribe(onNext:{ unread in
                debugLog(unread)
            })
            .disposed(by: disposeBag)
    }
}

// MARK: - 数据加载
extension WBHomePage {
    override func loadData() {
        super.loadData()
        isPullUp ? homeViewModel.loadStatuses(isPullUp: true) : homeViewModel.loadStatuses(isPullUp: false)
        isPullUp = false
    }
}
